#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using Premiere.DTO;
using System.Collections;
using Premiere.Window;
using Gizmox.WebGUI.Common.Interfaces;
using Gizmox.WebGUI.Common.Gateways;

#endregion

namespace PremiereMX
{
    public partial class MainForm : Form, IMainForm, IGatewayControl
    {
        UserDTO m_user;
        PeriodDTO m_period;
        TreeViewFormFactory m_factory;
        GeneralSetUpDTO m_generalSetup = new GeneralSetUpDTO();
        public MainForm()
        {
            InitializeComponent();
            InitalizeThemeMenu();
            splitContainerMain.Panel1.Enabled = false;
            panel1.Visible = false;
            m_factory =  new TreeViewFormFactory(this);
            //menuItemLogin_Click(null, null);
            initTab();
        }
        public RuleDTO GetRules(string formname)
        {
            RulesDTO rules = m_user.Rule;
            return (RuleDTO)rules.Rules[formname];
        }
        private void initTab()
        {
            foreach (string key in m_factory.MASTER.Items.Keys)
            {
                AddTabNavigationTab((TreeViewForm)m_factory.MASTER.Items[key]);
            }    
        }
        public void AddTabNavigationTab(TreeViewForm trv)
		{
            NavigationTab newtab = new Gizmox.WebGUI.Forms.NavigationTab();
            newtab.SuspendLayout();
            navigationTabs1.Controls.Add(newtab);
            newtab.CustomStyle = "Navigation";
            newtab.Dock = Gizmox.WebGUI.Forms.DockStyle.Fill;
            newtab.Extra = false;
            newtab.Label = trv.Text;
            newtab.Location = new System.Drawing.Point(0, 0);
            newtab.Name = trv.Text;
            newtab.Size = new System.Drawing.Size(205, 411);
            newtab.TabIndex = 0;
            newtab.Text = trv.Text;
            newtab.Controls.Add(CreateTreeView(trv));
		}
        private TreeView CreateTreeView(TreeViewForm trv)
        {
            TreeView treeView = new Gizmox.WebGUI.Forms.TreeView();
            treeView.ImageList = imageList1;
            treeView.BorderStyle = Gizmox.WebGUI.Forms.BorderStyle.None;
            treeView.BorderWidth = new Gizmox.WebGUI.Forms.BorderWidth(0);
            treeView.Dock = Gizmox.WebGUI.Forms.DockStyle.Fill;
            treeView.Location = new System.Drawing.Point(0, 0);
            treeView.Name = "treeView" + trv.Text;
            treeView.Size = new System.Drawing.Size(205, 411);
            treeView.TabIndex = 0;
            treeView.Font = new System.Drawing.Font("Calibri", 8.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));

            foreach (string trvi in trv.Items.Keys)
            {
                addNodes((TreeViewForm)trv.Items[trvi], treeView.Nodes);
            }
            return treeView;
        }
        public void addNodes(TreeViewForm t, TreeNodeCollection nodes)
        {
            TreeNode tnode = nodes.Add(t.Text);
            tnode.CheckBox = Gizmox.WebGUI.Forms.CheckBoxVisibility.Inherited;
            tnode.Name = t.Text;
            tnode.Text = t.Text;
            tnode.ToolTipText = t.Text;
            tnode.Tag = t;
            //tnode.AfterSelect += new TreeViewEventHandler(tnode_AfterSelect);
            tnode.NodeMouseClick += new TreeNodeMouseClickEventHandler(tnode_NodeMouseClick);
            if (t.Items.Count > 0)
            {
                tnode.ImageIndex = 0;
                foreach (string ti in t.Items.Keys)
                {
                    addNodes((TreeViewForm)t.Items[ti], tnode.Nodes);
                }
            }
            tnode.Collapse(true);
        }

        void tnode_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            tnode_AfterSelect(sender, null);
        }
        void tnode_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (m_user == null)
            {
                MessageBox.Show("Please Login First");
                return;
            }
            TreeNode td = (TreeNode)sender;
            TreeViewForm trv = (TreeViewForm)td.Tag;
            panelActiveForm.Controls.Clear();
            panelActiveForm.Text = string.Empty;
            toolBarMain.Buttons.Clear();
            UserControl control = m_factory.GetUserControlByID(trv.ID);
            if (control != null)
            {
                IUserControlBase uc = (IUserControlBase)control;
                panelActiveForm.Controls.Add(control);
                uc.Init();
                panelActiveForm.Text = trv.Text;
                uc.UpdateButton();
            }
            else
            {
                panelActiveForm.Controls.Add(CreateTreeView(trv));
                panelActiveForm.Text = trv.Text;
            }
        }
        private void menuItemLogin_Click(object sender, EventArgs e)
        {
            //panelActiveForm.Controls.Clear();
            //panelActiveForm.Text = string.Empty;
            //panelActiveForm.Controls.Add(new UserControlLogin(this));
            //panelActiveForm.Text = panelActiveForm.Controls[0].Text;
            if (menuItemLogin.Text == "Log Out")
            {
                m_user = null;
                statusBarPanelAdmin.Text = "";
                m_period = null;
                statusBarPanelPerion.Text = "";
                m_generalSetup = null;
                splitContainerMain.Panel1.Enabled = false;
                panel1.Visible = false;
                panelActiveForm.Controls.Clear();
                panelActiveForm.Text = string.Empty;
                toolBarMain.Buttons.Clear();
            }
                LoginForm frm = new LoginForm(this, UpdateUser);
                frm.ShowDialog();
                menuItemLogin.Text = "Log In";
        }
        #region IMainForm Members

        public void SetUser(UserDTO user)
        {
            m_user = user;
            statusBarPanelAdmin.Text = user.Name;
            splitContainerMain.Panel1.Enabled = true;
            panel1.Visible = true;
        }

        public UserDTO GetUser()
        {
            return m_user;
        }

        public void SetPeriod(PeriodDTO period)
        {
            m_period = period;
            statusBarPanelPerion.Text = period == null ? "active month not set" : period.ToString();
        }

        public PeriodDTO GetPeriod()
        {
            return m_period;
        }

        public void SetGeneralSetup(GeneralSetUpDTO generalSetup)
        {
            m_generalSetup = generalSetup;
        }
        public GeneralSetUpDTO GetGeneralSetup()
        {
            return m_generalSetup;
        }

        public void UpdateButton(IList buttons)
        {
            this.toolBarMain.Buttons.Clear();
            if (buttons == null) return;
            foreach (ToolBarButton button in buttons)
            {
                this.toolBarMain.Buttons.Add(button);
            }
        }

        #endregion

        private void menuItem7_Click(object sender, EventArgs e)
        {

        }
        private void InitalizeThemeMenu()
        {
            foreach (string strTheme in VWGContext.Current.Config.Themes)
            {
                MenuItem objThemeMenu = new MenuItem(strTheme);
                objThemeMenu.Tag = string.Format("Theme.{0}", strTheme);
                menuItemThemes.MenuItems.Add(objThemeMenu);
                objThemeMenu.Click += new EventHandler(menuItemUpdateTheme_Click);
            }
            UpdateThemeMenu();
        }
        private void UpdateThemeMenu()
        {
            foreach (MenuItem objMenuItem in menuItemThemes.MenuItems)
            {
                objMenuItem.RadioCheck = ((string)objMenuItem.Tag == VWGContext.Current.CurrentTheme);
            }
        }
        private void menuItemUpdateTheme_Click(object sender, EventArgs e)
        {
            if (sender is MenuItem)
                this.Context.CurrentTheme = ((MenuItem)sender).Tag.ToString();
        }

        #region IGatewayControl Members

        public void Print(DataSet ds)
        {
            LinkParameters objParams = new LinkParameters();
            objParams.Size = new Size(100, 100);
            objParams.Location = new Point(0, 0);
            m_printDS = ds;
            GatewayReference r = new GatewayReference(this, "test");
            Link.Open(r);
        }
        DataSet m_printDS;
        public IGatewayHandler GetGatewayHandler(IContext objContext, string strAction)
        {
            Utils.PrintDataSet(objContext, m_printDS);
            return null;
        }

        #endregion

        private void MainForm_Load(object sender, EventArgs e)
        {
            LoginForm frm = new LoginForm(this,UpdateUser);
            frm.ShowDialog();
        }
        void UpdateUser()
        {
            if (splitContainerMain.Panel1.Enabled)
                menuItemLogin.Text = "Log Out";
            else
                menuItemLogin.Text = "Log In";
        }
    }

}